package com.example.swd1.gudong.main.user.runningrank;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.swd1.gudong.R;

public class RunningRankActivity extends Activity implements View.OnClickListener {
    private RelativeLayout ll_running_rank_week;
    private RelativeLayout ll_running_rank_month;
    private RelativeLayout ll_running_rank_total;
    private TextView tv_running_rank_week_text;
    private TextView tv_running_rank_week_line;
    private TextView tv_running_rank_month_text;
    private TextView tv_running_rank_month_line;
    private TextView tv_running_rank_total_text;
    private TextView tv_running_rank_total_line;
    private ImageView iv_rank_back;
    private boolean weekTabChoosed;//Save click status
    private boolean monthTabChoosed;
    private boolean totalTabChoosed;
    private FragmentManager fm;
    private Fragment weekFragment;
    private Fragment monthFragment;
    private Fragment totalFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_running_rank);

        loadUIComponent();

        initFragment();

        initDisplay();
    }

    private void initDisplay() {
        onClick(ll_running_rank_week);
    }

    private void initFragment() {
        weekFragment = new RankFragment();
        monthFragment = weekFragment;
        totalFragment = weekFragment;
    }


    private void loadUIComponent() {
        ll_running_rank_week = (RelativeLayout) findViewById(R.id.ll_running_rank_week);
        ll_running_rank_month = (RelativeLayout) findViewById(R.id.ll_running_rank_month);
        ll_running_rank_total = (RelativeLayout) findViewById(R.id.ll_running_rank_total);
        tv_running_rank_week_text = (TextView) findViewById(R.id.tv_running_rank_week_text);
        tv_running_rank_week_line = (TextView) findViewById(R.id.tv_running_rank_week_line);
        tv_running_rank_month_text = (TextView) findViewById(R.id.tv_running_rank_month_text);
        tv_running_rank_month_line = (TextView) findViewById(R.id.tv_running_rank_month_line);
        tv_running_rank_total_text = (TextView) findViewById(R.id.tv_running_rank_total_text);
        tv_running_rank_total_line = (TextView) findViewById(R.id.tv_running_rank_total_line);
        iv_rank_back = (ImageView) findViewById(R.id.iv_rank_back);

        ll_running_rank_week.setOnClickListener(this);
        ll_running_rank_month.setOnClickListener(this);
        ll_running_rank_total.setOnClickListener(this);
        iv_rank_back.setOnClickListener(this);

        fm = getFragmentManager();
    }

    @Override
    public void onClick(View v) {
        if (v == ll_running_rank_week) {
            if (weekTabChoosed == false) {
                if (monthTabChoosed) {
                    changeChoosedTab(tv_running_rank_month_text, tv_running_rank_month_line, false);
                }
                if (totalTabChoosed) {
                    changeChoosedTab(tv_running_rank_total_text, tv_running_rank_total_line, false);
                }
                changeChoosedTab(tv_running_rank_week_text, tv_running_rank_week_line, true);
                weekTabChoosed = true;
                monthTabChoosed = false;
                totalTabChoosed = false;

                FragmentTransaction ft = fm.beginTransaction();
                ft.replace(R.id.fl_rank, weekFragment);
                ft.commit();
            }
            return;
        }
        if (v == ll_running_rank_month) {
            if (monthTabChoosed == false) {
                if (weekTabChoosed) {
                    changeChoosedTab(tv_running_rank_week_text, tv_running_rank_week_line, false);
                }
                if (totalTabChoosed) {
                    changeChoosedTab(tv_running_rank_total_text, tv_running_rank_total_line, false);
                }
                weekTabChoosed = false;
                monthTabChoosed = true;
                totalTabChoosed = false;
                changeChoosedTab(tv_running_rank_month_text, tv_running_rank_month_line, true);

                FragmentTransaction ft = fm.beginTransaction();
                ft.replace(R.id.fl_rank, monthFragment);
                ft.commit();
            }
            return;
        }
        if (v == ll_running_rank_total) {
            if (totalTabChoosed == false) {
                if (weekTabChoosed) {
                    changeChoosedTab(tv_running_rank_week_text, tv_running_rank_week_line, false);
                }
                if (monthTabChoosed) {
                    changeChoosedTab(tv_running_rank_month_text, tv_running_rank_month_line, false);
                }
                changeChoosedTab(tv_running_rank_total_text, tv_running_rank_total_line, true);
                weekTabChoosed = false;
                monthTabChoosed = false;
                totalTabChoosed = true;

                FragmentTransaction ft = fm.beginTransaction();
                ft.replace(R.id.fl_rank, totalFragment);
                ft.commit();
            }
            return;
        }
        if (v == iv_rank_back) {
            finish();
        }
    }

    private void changeChoosedTab(TextView text, TextView line, boolean state) {
        if (state) {
            text.setTextColor(getResources().getColor(R.color.green_grid));
            line.setVisibility(View.VISIBLE);
            line.setBackgroundColor(getResources().getColor(R.color.green_grid));
        } else {
            text.setTextColor(getResources().getColor(R.color.button_material_dark));
            line.setVisibility(View.INVISIBLE);
        }
    }
}
